package demo_leetcode_day;

/*
给定两个二叉树，编写一个函数来检验它们是否相同。
如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。
示例 1:
输入:       1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

输出: true
示例 2:

输入:      1          1
          /           \
         2             2

        [1,2],     [1,null,2]

输出: false
示例 3:

输入:       1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]
输出: false
*/

import java.util.ArrayList;
import java.util.List;

public class demo_20200807 {
    public static void main(String[] args) {
        TreeNode nodeA1 = new TreeNode(1);
        TreeNode nodeA2 = new TreeNode(2);
        TreeNode nodeA3 = new TreeNode(3);

        TreeNode nodeB1 = new TreeNode(1);
        TreeNode nodeB2 = new TreeNode(2);
        TreeNode nodeB3 = new TreeNode(3);

        System.out.println(new Solution().isSameTree(nodeA1, nodeB1));
    }

     static class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode() {}
         TreeNode(int val) { this.val = val; }
         TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
     }

    static class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            return checkNode(p, q);
        }
        private boolean checkNode(TreeNode p, TreeNode q){
            if(null == p && null == q) return true;
            if(null == p || null == q) return false;
            if(p.val != q.val) return false;
            return checkNode(p.left, q.left) && checkNode(p.right, q.right);
        }
    }
}
